package com.puntodonna.fogliocassa.client.widget;

import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.Header;
import com.google.gwt.user.cellview.client.SafeHtmlHeader;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SingleSelectionModel;
import com.puntodonna.fogliocassa.shared.FoglioCassaObj;
import com.puntodonna.fogliocassa.shared.MovimentoObj;

public class FogliCassaDataTable extends AbstractDataTableWithPager<FoglioCassaObj> {

	public static final ProvidesKey<FoglioCassaObj> KEY_PROVIDER = new ProvidesKey<FoglioCassaObj>() {
		public Object getKey(FoglioCassaObj item) {
			return item == null ? null : item.getId();
		}
	};

	public FogliCassaDataTable() {
		super(new ListDataProvider<FoglioCassaObj>(KEY_PROVIDER));
		setSelectionModel(new SingleSelectionModel<FoglioCassaObj>(KEY_PROVIDER), DefaultSelectionEventManager.<FoglioCassaObj> createCheckboxManager());
		setSize("100%", "100%");
		setEmptyTableWidget(new Label("Nessun foglio cassa da visualizzare!"));
	}

	@SuppressWarnings("unchecked")
	public SingleSelectionModel<FoglioCassaObj> getSingleSelectionModel() {
		return (SingleSelectionModel<FoglioCassaObj>) getSelectionModel();
	}

	@Override
	public void initializeColumns() {
		setAutoHeaderRefreshDisabled(true);
		
		final NumberFormat twoDForm = NumberFormat.getDecimalFormat();

		Column<FoglioCassaObj, Boolean> checkColumn = new Column<FoglioCassaObj, Boolean>(new CheckboxCell(true, false)) {
			@Override
			public Boolean getValue(FoglioCassaObj object) {
				return getSelectionModel().isSelected(object);
			}
		};
		addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));
		setColumnWidth(checkColumn, 12, Unit.PCT);

		// MOVIMENTI
		Header<String> lordoSum = new Header<String>(new TextCell()) {
			@Override
			public String getValue() {
				List<FoglioCassaObj> items = getVisibleItems();
				double sum = 0;
				for (FoglioCassaObj item : items) {
					for (Iterator<MovimentoObj> iterator = item.getMovimenti().values().iterator(); iterator.hasNext();) {
						MovimentoObj movimento = (MovimentoObj) iterator.next();
						sum += movimento.getLordo();
					}
				}
				if (sum != 0)
					sum = Math.round(sum * 100.0) / 100.0;
				return "Σ Lordo: " + twoDForm.format(sum);
			}
		};
		
		Column<FoglioCassaObj, String> nomeColumn = new Column<FoglioCassaObj, String>(new TextCell()) {
			@Override
			public String getValue(FoglioCassaObj object) {
				return object.getNome();
			}
		};
		nomeColumn.setSortable(true);
		getSortHandler().setComparator(nomeColumn, new Comparator<FoglioCassaObj>() {
			public int compare(FoglioCassaObj o1, FoglioCassaObj o2) {
				return o1.getNome().compareTo(o2.getNome());
			}
		});
		addColumn(nomeColumn, new SafeHtmlHeader(SafeHtmlUtils.fromSafeConstant("Nome")), lordoSum);
		
		Column<FoglioCassaObj, String> aperturaColumn = new Column<FoglioCassaObj, String>(new TextCell()) {
			@Override
			public String getValue(FoglioCassaObj object) {
				Date openDate = object.getDataApertura();
				if(openDate == null)
					return "notSetted";
				
				return DateTimeFormat.getFormat("dd/MM/yyyy HH:mm:ss").format(openDate);
			}
		};
		aperturaColumn.setSortable(true);
		getSortHandler().setComparator(aperturaColumn, new Comparator<FoglioCassaObj>() {
			public int compare(FoglioCassaObj o1, FoglioCassaObj o2) {
				return o1.getDataApertura().compareTo(o2.getDataApertura());
			}
		});
		addColumn(aperturaColumn, new SafeHtmlHeader(SafeHtmlUtils.fromSafeConstant("Aperto il")));
		
		Column<FoglioCassaObj, String> chiusuraColumn = new Column<FoglioCassaObj, String>(new TextCell()) {
			@Override
			public String getValue(FoglioCassaObj object) {
				Date closeDate = object.getDataChiusura();
				if(closeDate == null)
					return "";
				
				return DateTimeFormat.getFormat("dd/MM/yyyy HH:mm:ss").format(closeDate);
			}
		};
		chiusuraColumn.setSortable(true);
		getSortHandler().setComparator(chiusuraColumn, new Comparator<FoglioCassaObj>() {
			public int compare(FoglioCassaObj o1, FoglioCassaObj o2) {
				return o1.getDataChiusura().compareTo(o2.getDataChiusura());
			}
		});
		addColumn(chiusuraColumn, new SafeHtmlHeader(SafeHtmlUtils.fromSafeConstant("Chiuso il")));
		
		getColumnSortList().push(aperturaColumn);
	}
}